const action = require('../action');
const apiPrefix = require('../config').apiPrefix;
const APIError = require('../module/rest').APIError;

module.exports = {
  'GET /api/v2/studio-list': async (ctx, next) => {
    let returnData = await action.studio.getAll();
    if (returnData.result) {
      ctx.rest({
        "studio": returnData.data
      });
    } else {
      throw new APIError("studio:not_found", "studio not found by id.");
    }
  },

  'GET /api/v2/studio/:id': async (ctx, next) => {
    let returnData = await action.studio.getById(ctx.params.id);
    if (returnData.result) {
      ctx.rest({
        "studio": returnData.data
      });
    } else {
      throw new APIError("studio:not_found", "studio not found by id.");
    }
  },

  'POST /api/v2/studio': async (ctx, next) => {
    let returnData = await action.studio.create(ctx.request.body.studio);
    if (returnData.result) {
      ctx.rest({
        "studio": returnData.data
      });
    } else {
      throw new APIError("studio:create_failed", "create studio failed");
    }
  },

  'DELETE /api/v2/studio/:id': async (ctx, next) => {
    let returnData = await action.studio.deleteById(ctx.params.id);
    if (returnData.result) {
      ctx.rest({
        "result": true
      });
    } else {
      throw new APIError("studio:delete_failed", "delete studio failed");
    }
  },

  'PUT /api/v2/studio/:id/desc': async (ctx, next) => {
    let returnData = await action.studio.updatePropById("desc", ctx.params.id, ctx.request.body.desc);
    if (returnData.result) {
      ctx.rest({
        "result": true
      });
    } else {
      throw new APIError("studio:update_failed", "studio not updated");
    }
  },

  'POSTFILE /api/v2/studio/:id/uploadFile': async (ctx, next) => {
    
    console.log(ctx.req.file.filename);
    ctx.rest({
      "filename": ctx.req.file.filename
    });
    // console.log(ctx.req)
  },

  'POSTFILE /api/v2/studio/:id/studioBanner': async (ctx, next) => {
    let returnData = await action.studio.updatePropById("studioBanner", ctx.params.id, ctx.req.file.filename);
    if (returnData.result) {
      ctx.rest({
        "result": true
      });
    } else {
      throw new APIError("studio:upload_failed", "file not upload");
    }
  },


};